
/*
 * GET home page.
 */
var crypto=require('crypto');
var Auser=require('../models/auser');       //后台用户
var User=require('../models/user');
var Post=require('../models/post');
var Page=require('../models/page');
var BbsPlate=require('../models/bbsPlate');
var Intro=require('../models/intro');
var Active=require('../models/avtive');
var Coffee=require('../models/coffee');
var Province=require('../models/province');
var City=require('../models/city');
var Area=require('../models/area');
var async=require('async');
var fs=require('fs');
var dateFormat = require('dateformat');
var formidable=require('formidable');
var ActiveApply=require('../models/activeApply');
 module.exports = function(app){
     //后台管理
     app.get('/manager',function(req,res){
             res.render('manager/login',{
                 uname:'',
                 pwd:''
             })
     })
     app.post('/manager/login',function(req,res){
         var username=req.body.username;
         var md5 = crypto.createHash('md5');
         var password = md5.update(req.body.password).digest('base64');
         var auser=new Auser();
         auser.username=username;
         auser.password=password;
         new Auser().findAllUser(function(err,datas){
             if(datas.length==0){
                 if(username!='admin'){
                     return res.render('manager/login',{
                         uname:username,
                         pwd:req.body.password,
                         error:'第一次登陆请使用admin登陆,以后登陆密码为初次登陆时的密码！'
                     });
                 }else{
                     auser.save(function(err){
                         if(err){
                             return res.render('manager/login',{
                                 uname:username,
                                 pwd:req.body.password,
                                 error:'服务器繁忙，请稍后再试！'
                             });
                         }
                         req.session.auser=auser;
                         return res.redirect('/manager/main');
                     })
                 }
             }
             auser.findUserByName(username,function(err,data){
                 if(err){
                     return res.render('manager/login',{
                         uname:username,
                         pwd:req.body.password,
                         error:'服务器繁忙，请稍后再试！'
                     });
                 }
                 if(data){
                     if(password!=data.password){
                         return res.render('manager/login',{
                             uname:username,
                             pwd:req.body.password,
                             error:'用户名密码不正确，请重新输入！'
                         });
                     }
                     req.session.auser=data;
                     return res.redirect('/manager/main');
                 }
                 return res.render('manager/login',{
                     uname:username,
                     pwd:req.body.password,
                     error:'用户名不存在，请重新输入'
                 });
             })
         })
     })

     app.get('/manager/loginout',function(req,res){
         req.session.auser=null;
         res.redirect('/manager');
     })

     app.get('/manager/main',function(req,res){
         res.render('manager/main',{
         })
     })

     app.get('/manager/coffee',function(req,res){
         var page=new Page(req.query.num);
         var obj={
             page:page,
             search:{}
         };
         async.waterfall([
             function(callback){
                 new Coffee().findForPage(obj,function(err,count,pageCount,datas){
                     callback(err, count, pageCount,datas);
                 })

             },
             function(count, pageCount, datas,callback){
                 new Province().findAllProvince(function(err,provinceDatas){
                     callback(err,count,pageCount,datas,provinceDatas);
                 })
             }
         ], function (err, count,pageCount,datas,provinceDatas) {
             if(err){
                 req.flash('error',err);
                 return res.redirect('back');
             }

             page.pageCount=pageCount;
             res.render('manager/coffee',{
                 title:'咖啡厅列表',
                 datas:datas,
                 page:page,
                 count:count,
                 provinceDatas:provinceDatas
             })
         });
     })
     app.get('/manager/addCoffee',function(req,res){
         new Province().findAllProvince(function(err,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back');
             }
             res.render('manager/addCoffee',{
                 datas:datas,
                 title:'咖啡厅添加'
             })
         })

     })
     app.get('/manager/loadCity',function(req,res){
         if(req.query.pid==""){
             var datas=[];
             res.render('manager/loadData',{
                 datas:datas
             })
         }else{
             new City().findCityByPid(req.query.pid,function(err,datas){
                 if(err){
                     console.log(err)
                 }
                 res.render('manager/loadData',{
                     datas:datas
                 })
             })
         }

     })
     app.get('/manager/loadArea',function(req,res){
         if(req.query.cid==""){
             var datas=[];
             res.render('manager/loadData',{
                 datas:datas
             })

         }else{
             new Area().findAreaByCid(req.query.cid,function(err,datas){
                 if(err){
                     console.log(err)
                 }
                 res.render('manager/loadData',{
                     datas:datas
                 })
             })
         }

     })
     app.post('/manager/addCoffee',function(req,res){
         if(req.body.province==''||req.body.city==''||req.body.area==''){
             req.flash('error','请正确选择省市县')
             return res.redirect('back');
         }
         var coffee=new Coffee();
         coffee.name=req.body.name;
         coffee.intro=req.body.intro;
         coffee.province=req.body.province;
         coffee.city=req.body.city;
         coffee.area=req.body.area;
         coffee.addr=req.body.addr;
         coffee.save(function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','添加成功');
             }
             return res.redirect('/manager/coffee');
         })
     })
     app.post('/manager/coffeeEdit',function(req,res){
         if(req.body.province==''||req.body.city==''||req.body.area==''){
             req.flash('error','请正确选择省市县')
             return res.redirect('back');
         }
         var options={name:req.body.name,intro:req.body.intro,addr:req.body.addr,province:req.body.province,city:req.body.city,area:req.body.area}
         new Coffee().updateCoffeeById(req.body.id,options,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','修改成功');
             }
             return res.redirect('back');

         })
     })
     app.get('/manager/delCoffee',function(req,res){
         new Coffee().delCoffeeById(req.query.id,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','删除成功');
             }
             return res.redirect('back');
         })
     })
     app.post('/manager/edit',function(req,res){
         var options={title:req.body.title,content:req.body.content,endEditTime:new Date()}

         new Intro().updateIntro(options,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','编辑成功');
             }
             return res.redirect('back');

         })
     })

     app.get('/manager/active',function(req,res){
         var page=new Page(req.query.num);
         var obj={
             page:page,
             search:{}
         };
        new Active().findForPage(obj,function(err,count,pageCount,datas){
            if(err){
                req.flash('error',err);
                return res.redirect('back');
            }
            page.pageCount=pageCount;
            res.render('manager/active',{
                title:'活动编辑',
                datas:datas,
                page:page,
                count:count
            })
        })

     })

     app.get('/manager/addActive',function(req,res){
         var datas=[];
         res.render('manager/addActive',{
             title:'活动添加'
         })
     })
     app.post('/manager/addActive',function(req,res){
         var active=new Active();
         active.activeDate=req.body.activeDate;
         active.addr=req.body.addr;
         active.theme=req.body.theme;
         active.discribe=req.body.discribe;
         active.language=req.body.language;
         active.cost=req.body.cost;
         active.save(function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','添加成功');
             }
             res.redirect('/manager/active');
         })

     })
     app.post('/manager/activeEdit',function(req,res){
         var options={addr:req.body.addr,cost:req.body.cost,language:req.body.language,theme:req.body.theme,discribe:req.body.discribe,activeDate:req.body.activeDate};
         new Active().updateActiveById(req.body.id,options,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','修改成功');
             }
             res.redirect('back');
         })

     })

     app.get('/manager/delActive',function(req,res){
         new Active().delActiveById(req.query.id,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','删除成功');
             }
             res.redirect('back');
         })

     })

     app.get('/manager/registerUser',function(req,res){
         var page=new Page(req.query.num);
         var obj={
             page:page,
             search:{}
         };
         new User().findForPage(obj,function(err,count,pageCount,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back');
             }
             page.pageCount=pageCount
             res.render('manager/registerUser',{
                 title:'注册用户',
                 datas:datas,
                 page:page,
                 count:count
             });
         })
     })

     app.get('/manager/updateUserState',function(req,res){
         new User().updateStateById(req.query.id,req.query.state,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','更改用户状态成功');
             }
             res.redirect('back');
         })

     })

     app.get('/manager/bbsPlate',function(req,res){
         new BbsPlate().findAllPlate(function(err,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back')
             }
             res.render('manager/bbsPlate',{
                 title:'论坛板块管理',
                 datas:datas
             })

         })

     })

     app.get('/manager/addPlate',function(req,res){
         res.render('manager/addPlate',{
             title:'论坛板块添加'
         })
     })
     app.post('/manager/addPlate',function(req,res){
         var bbsPlate=new BbsPlate();
         bbsPlate.platename=req.body.platename;
         bbsPlate.describe=req.body.describe;
         bbsPlate.save(function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','添加成功')
             }
             res.redirect('/manager/bbsPlate')
         });
     })
     app.post('/manager/plateEdit',function(req,res){
         var options={platename:req.body.platename,describe:req.body.describe}
         new BbsPlate().updatePlateById(req.body.id,options,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','修改成功')
             }
             res.redirect('back')
         });
     })

     app.get('/manager/delPlate',function(req,res){
         var options={bbsPlate:req.query.id}
         new Post().findPostByOptions(options,function(err,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back')
             }
             if(datas.length!=0){
                 req.flash('error','该板块下有内容不能删除！！！');
                 return res.redirect('back')
             }
             new BbsPlate().delPlateById(req.query.id,function(err){
                 if(err){
                     req.flash('error',err);
                 }else{
                     req.flash('success','删除成功')
                 }
                 res.redirect('back')
             });
         })

     })

     app.get('/manager/postList',function(req,res){
         var page=new Page(req.query.num);
         var obj={
             page:page,
             search:{}
         };
         new Post().findForPage(obj,function(err,count,pageCount,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back');
             }
             page.pageCount=pageCount
             res.render('manager/postList',{
                 title:'注册用户',
                 datas:datas,
                 page:page,
                 count:count
             });
         })

     })

     app.get('/manager/groupPost',function(req,res){
         var page=new Page(req.query.num);
         var obj={
             page:page,
             search:{bbsPlate:req.query.id}
         };
         new Post().findForPage(obj,function(err,count,pageCount,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back');
             }
             page.pageCount=pageCount
             res.render('manager/groupPostList',{
                 title:'注册用户',
                 datas:datas,
                 page:page,
                 count:count
             });
         })

     })

     app.get('/manager/updatePostState',function(req,res){
         new Post().updateStateById(req.query.id,req.query.state,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','操作成功');
             }
             res.redirect('back');
         })

     })
     app.get('/manager/applyUsers',function(req,res){
         var page=new Page(req.query.num);
         var obj={
             page:page,
             search:{applyActive:req.query.id}
         };
         new ActiveApply().findForPage(obj,function(err,count,pageCount,datas){
             if(err){
                 req.flash('error',err);
                 return res.redirect('back');
             }
             page.pageCount=pageCount
             res.render('manager/applyUsers',{
                 title:'注册用户',
                 datas:datas,
                 page:page,
                 count:count
             });
         })
     })
     app.get('/manager/updateActiveState',function(req,res){
         var options={state:req.query.state};
         new ActiveApply().updateActiveApplyById(req.query.id,options,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','操作成功');
             }
             res.redirect('back');

         })
     })
     app.post('/manager/batchUpdateActiveState',function(req,res){
         var ids=req.body.ids;
         if(typeof (ids)==typeof ('a')){
             ids=[ids];
         }
         var opt={_id:{"$in":ids}};
         var options={state:req.query.state};
         console.log(typeof (ids))
         console.log(ids)
         new ActiveApply().updateActiveApplyByOpt(opt,options,function(err){
             if(err){
                 req.flash('error',err);
             }else{
                 req.flash('success','批量操作成功');
             }
             res.redirect('back');
         })
//         new ActiveApply().findActiveApplyByOptions(opt,function(err,datas){
//            console.log(datas)
//         })
     })
     //使用kindEditor图片上传的处理代码
     app.post('/manager/uploadImage',function(req,res){

     })

 };
